<?php


/**
 * This file contains the class for validate ipv4 addresses
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @version 1
 * @package core
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * class that validates ipv4 addresses
 * <p>use it:
 * <code>
 * validIPv4::validate($string)
 * </code>
 * returning true or false
 * </p>
 * @version 1
 */
class validateIPv4
{

	/**
	 * check a ipv4 for validity
	 *
	 * @param string	string to validate
	 * @return bool
	 */
	public static function validate ($string = null)
	{
		$string = trim($string);
        if (!$string)
            return false;
        // explode in segments
        $segs = explode ('.', $string);
        if (count ($segs) != 4)
            return false;
        // check if each segm. is within 0 and 255
        foreach ($segs as $temp)
            if ((int)$temp < 0 || (int)$temp > 255)
                return false;
        // all fine
        return true;
	}
}